import 'package:backend.core/core/domain/entities/app_store/app_store_purchased_subscription_transaction.dart';
import 'package:backend.core/core/domain/entities/app_store/app_store_transaction_revocation_info.dart';
import 'package:backend.purchases/domain/entities/app_store_api/app_store_api_environment.dart';
import 'package:backend.purchases/domain/entities/app_store_api/app_store_api_purchased_subscription_transaction.dart';
import 'package:backend.purchases/domain/helpers/app_store/create_app_store_offer_type_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/app_store/create_app_store_ownership_type_from_api_one.dart';
import 'package:backend.purchases/domain/helpers/app_store/create_app_store_revocation_info_from_api_one.dart';

abstract class CreateAppStorePurchasedSubscriptionTransactionFromApiOne {
  AppStorePurchasedSubscriptionTransaction call({
    required AppStoreApiSubscriptionTransaction apiPurchasedSubscriptionTransaction,
    required String appStoreUserId,
  });
}

class CreateAppStorePurchasedSubscriptionTransactionFromApiOneImpl
    implements CreateAppStorePurchasedSubscriptionTransactionFromApiOne {
  const CreateAppStorePurchasedSubscriptionTransactionFromApiOneImpl({
    required CreateAppStoreTransactionOfferTypeFromApiOne
        createAppStoreTransactionOfferTypeFromApiOne,
    required CreateAppStoreTransactionOwnershipTypeFromApiOne
        createAppStoreTransactionOwnershipTypeFromApiOne,
    required CreateAppStoreTransactionRevocationInfoFromApiOne
        createAppStoreTransactionRevocationInfoFromApiOne,
  })  : _createAppStoreTransactionOfferTypeFromApiOne =
            createAppStoreTransactionOfferTypeFromApiOne,
        _createAppStoreTransactionOwnershipTypeFromApiOne =
            createAppStoreTransactionOwnershipTypeFromApiOne,
        _createAppStoreTransactionRevocationInfoFromApiOne =
            createAppStoreTransactionRevocationInfoFromApiOne;

  final CreateAppStoreTransactionOfferTypeFromApiOne _createAppStoreTransactionOfferTypeFromApiOne;
  final CreateAppStoreTransactionOwnershipTypeFromApiOne
      _createAppStoreTransactionOwnershipTypeFromApiOne;
  final CreateAppStoreTransactionRevocationInfoFromApiOne
      _createAppStoreTransactionRevocationInfoFromApiOne;

  @override
  AppStorePurchasedSubscriptionTransaction call({
    required AppStoreApiSubscriptionTransaction apiPurchasedSubscriptionTransaction,
    required String appStoreUserId,
  }) {
    final offerType = _createAppStoreTransactionOfferTypeFromApiOne(
      apiOfferType: apiPurchasedSubscriptionTransaction.offerType,
    );

    final ownershipType = _createAppStoreTransactionOwnershipTypeFromApiOne(
      apiOwnershipType: apiPurchasedSubscriptionTransaction.ownershipType,
    );

    AppStoreTransactionRevocationInfo? revocationInfo;

    if (apiPurchasedSubscriptionTransaction.revocationInfo != null) {
      revocationInfo = _createAppStoreTransactionRevocationInfoFromApiOne(
        apiRevocationInfo: apiPurchasedSubscriptionTransaction.revocationInfo!,
      );
    }

    return AppStorePurchasedSubscriptionTransaction(
      id: apiPurchasedSubscriptionTransaction.id,
      time: apiPurchasedSubscriptionTransaction.time,
      appStoreUserId: appStoreUserId,
      originalTransactionId: apiPurchasedSubscriptionTransaction.originalTransactionId,
      purchaseItemSku: apiPurchasedSubscriptionTransaction.purchaseItemSku,
      offerType: offerType,
      ownershipType: ownershipType,
      test: apiPurchasedSubscriptionTransaction.environment == AppStoreApiEnvironment.sandbox,
      revocationInfo: revocationInfo,
      expirationTime: apiPurchasedSubscriptionTransaction.expirationTime,
    );
  }
}
